package com.goods.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.entities.Product;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.service.ProductService;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @author Decheng Kong
 * @date 2021/11/8 18:32
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;

    @Override
    public PageVO<ProductVO> findProductList(Integer pageNum,
                                             Integer pageSize,
                                             String name,
                                             Integer categoryId,
                                             String supplier,
                                             Integer status, Integer[] categorys) {

        PageHelper.startPage(pageNum, pageSize);

        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(name)) {
            criteria.andLike("name", "%" + name + "%");
        }

        if (categoryId != null) {
            criteria.andEqualTo("threeCategoryId", categoryId);
        }

        if (!StringUtils.isEmpty(supplier)) {
            criteria.andLike("remark", supplier);
        }

        if (status != null) {
            criteria.andEqualTo("status", status);
        }

        if (categorys != null && categorys.length > 0) {
            if (categorys.length == 1) criteria.andEqualTo("oneCategoryId", categorys[0]);
            else if (categorys.length == 2) criteria.andEqualTo("twoCategoryId", categorys[1]);
            else if (categorys.length == 3) criteria.andEqualTo("threeCategoryId", categorys[2]);
        }

        List<Product> products = productMapper.selectByExample(example);
        List<ProductVO> collect = products.stream().map(product -> {
            ProductVO productVO = new ProductVO();
            BeanUtils.copyProperties(product, productVO);
            return productVO;
        }).collect(Collectors.toList());

        PageInfo<Product> info = new PageInfo<>(products);
        return new PageVO<>(info.getTotal(), collect);
    }

    @Override
    public void addProduct(ProductVO productVO) {
        Long[] categoryKeys = productVO.getCategoryKeys();
        productVO.setOneCategoryId(categoryKeys[0]);
        productVO.setTwoCategoryId(categoryKeys[1]);
        productVO.setThreeCategoryId(categoryKeys[2]);
        productVO.setPNum(UUID.randomUUID().toString());
        productVO.setCreateTime(new Date());
        productVO.setModifiedTime(productVO.getCreateTime());
        // 待审核状态
        productVO.setStatus(2);
        Product product = new Product();
        BeanUtils.copyProperties(productVO, product);

        productMapper.insert(product);
    }

    @Override
    public ProductVO getProductById(Integer id) {

        Product product = productMapper.selectByPrimaryKey(id);
        ProductVO productVO = new ProductVO();
        BeanUtils.copyProperties(product, productVO);
        return productVO;
    }

    @Override
    public void updateProduct(ProductVO productVO) {
        Product product = new Product();
        BeanUtils.copyProperties(productVO, product);
        product.setModifiedTime(new Date());
        product.setStatus(2);
        productMapper.updateByPrimaryKey(product);
    }

    @Override
    public void deleteProductById(Long id) {
        productMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void updateProductStatus(Long id, int code) {
        Product product = productMapper.selectByPrimaryKey(id);
        product.setStatus(code);
        product.setModifiedTime(new Date());
        productMapper.updateByPrimaryKey(product);
    }
}
